package com.hongdee.insurance.company.controller;

import com.hongdee.base.entity.ResultEntity;
import com.hongdee.common.util.StringUtil;
import com.hongdee.insurance.company.service.ITInsuranceCompanyBillService;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 王帅
 * @className TInsuranceCompanyBillController
 * @type class
 * @description
 * @path com.hongdee.insurance.company.controller
 * @create 2017/6/14 8:46
 **/
@Controller
@RequestMapping("/insurance/company/bill/detail")
public class TInsuranceCompanyBillDetailController {


    @Autowired
    private ITInsuranceCompanyBillService billService;

    /**
     * 分页查询对账单信息明细查询
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/page")
    @ResponseBody
    public ResultEntity page(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "start", defaultValue = "") String start,
                             @RequestParam(name = "limit", defaultValue = "") String limit,
                             @RequestParam(name = "billId", defaultValue = "") String billId,
                             @RequestBody Map data) throws Exception {
        if("".equals(billId)){
            throw new Exception("请选择对账单!!");
        }
        ResultEntity res=new ResultEntity();
        Map map=this.billService.billDetailPage(uid,billId,data,start,limit);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询成功!!");
        return res;
    }

    /**
     * 添加保单至结算单表
     * @param uid
     * @param token
     * @param data
     * @return
     */
    @RequestMapping("/addInsuranceOrder")
    @ResponseBody
    public ResultEntity addInsuranceOrder(@RequestParam(name = "uid", defaultValue = "") String uid,
                                          @RequestParam(name = "token", defaultValue = "") String token,
                                          @RequestParam(name = "billId", defaultValue = "") String billId,
                                          @RequestBody List<Map> data) throws Exception {
        ResultEntity res=new ResultEntity();
        this.billService.addInsuranctOrderToBalance(uid,billId,data);
        res.setSuccess(true);
        res.setMsg("添加保单，并自动结算完成!!");
        return res;
    }

    /**
     * 差异明细查询
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @param billId
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/diff/page")
    @ResponseBody
    public ResultEntity diffPage(@RequestParam(name = "uid", defaultValue = "") String uid,
                                 @RequestParam(name = "token", defaultValue = "") String token,
                                 @RequestParam(name = "start", defaultValue = "") String start,
                                 @RequestParam(name = "limit", defaultValue = "") String limit,
                                 @RequestParam(name = "billId", defaultValue = "") String billId,
                                 @RequestBody Map data) throws Exception {
        if("".equals(billId)){
            throw new Exception("请选择对账单!!");
        }
        ResultEntity res=new ResultEntity();
        Map map=this.billService.diffDetailPage(uid,billId,start,limit,data);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询成功!!");
        return res;
    }

    /**
     * 差异信息 根据条件 自动结算
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @param billId
     * @param data
     * @return
     */
    @RequestMapping("/diff/balance")
    @ResponseBody
    public ResultEntity diffBalance(@RequestParam(name = "uid", defaultValue = "") String uid,
                                    @RequestParam(name = "token", defaultValue = "") String token,
                                    @RequestParam(name = "start", defaultValue = "") String start,
                                    @RequestParam(name = "limit", defaultValue = "") String limit,
                                    @RequestParam(name = "billId", defaultValue = "") String billId,
                                    @RequestBody Map data) throws Exception {
        if("".equals(billId)){
            throw new Exception("请选择对账单!!");
        }
        ResultEntity res=new ResultEntity();
        this.billService.diffBalanceDetail(uid,billId,data);
        res.setSuccess(true);
        res.setMsg("自动结算成功!!");
        return res;
    }

    @RequestMapping("/diff/balance/company")
    @ResponseBody
    public Map diffBalance(@RequestParam(name = "uid", defaultValue = "") String uid,
                                    @RequestParam(name = "token", defaultValue = "") String token,
                                    @RequestParam(name = "billId", defaultValue = "") String billId) throws Exception {
        if("".equals(billId)){
            throw new Exception("请选择对账单!!");
        }
        Map map=new HashMap();
        List<Map<String,Object>> mapList=billService.diffBalanceCompanys(billId);
        map.put("msg","查询成功");
        map.put("success",true);
        map.put("dataList",mapList);
        return map;
    }

    /**
     * 调整保险公司保费和费率
     * @return
     */
    @RequestMapping("/diff/balance/tax")
    @ResponseBody
    public ResultEntity diffBalanceInsuranceCom(@RequestParam(name = "uid", defaultValue = "") String uid,
                                                @RequestParam(name = "token", defaultValue = "") String token,
                                                @RequestParam(name = "billId", defaultValue = "") String billId,
                                                @RequestBody Map<String, Object> data) throws Exception {
        if("".equals(billId)){
            throw new Exception("请选择对账单!!");
        }
        this.billService.diffBalanceInsuranceCom(billId,uid,data);
        ResultEntity res=new ResultEntity();
        res.setSuccess(true);
        res.setMsg("调整成功，请继续结算!!");
        return res;
    }

    @RequestMapping("/diff/balance/log")
    @ResponseBody
    public ResultEntity diffBalanceLogs(@RequestParam(name = "uid", defaultValue = "") String uid,
                                        @RequestParam(name = "token", defaultValue = "") String token,
                                        @RequestParam(name = "billId", defaultValue = "") String billId) throws Exception {
        if("".equals(billId)){
            throw new Exception("请选择对账单!!");
        }
        ResultEntity res=new ResultEntity();
        res.setSuccess(true);
        res.setData(this.billService.diffBalanceInsuranceComLogs(billId));
        return res;
    }


    /**
     * 差异明细 批量自动结算
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @param billId
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/diff/balances")
    @ResponseBody
    public ResultEntity diffBalances(@RequestParam(name = "uid", defaultValue = "") String uid,
                                    @RequestParam(name = "token", defaultValue = "") String token,
                                    @RequestParam(name = "start", defaultValue = "") String start,
                                    @RequestParam(name = "limit", defaultValue = "") String limit,
                                    @RequestParam(name = "billId", defaultValue = "") String billId,
                                    @RequestBody Map data) throws Exception {
        if("".equals(billId)){
            throw new Exception("请选择对账单!!");
        }
        ResultEntity res=new ResultEntity();
        this.billService.diffBalanceDetails(uid,billId,data);
        res.setSuccess(true);
        res.setMsg("自动结算成功!!");
        return res;
    }

    /**
     * 更新结算单信息 并结算
     * @param uid
     * @param token
     * @param billId
     * @param data
     * @return
     */
    @RequestMapping("/diffBalanceAdjust")
    @ResponseBody
    public ResultEntity diffBalanceAdjust(@RequestParam(name = "uid", defaultValue = "") String uid,
                                       @RequestParam(name = "token", defaultValue = "") String token,
                                       @RequestParam(name = "billId", defaultValue = "") String billId,
                                       @RequestBody Map data) throws Exception {
        if("".equals(billId)){
            throw new Exception("请选择对账单!!");
        }
        ResultEntity res=new ResultEntity();
        this.billService.diffBalanceDetailAdjust(uid,billId,data);
        res.setSuccess(true);
        res.setMsg("更新并结算成功!!");
        return res;
    }

    /**
     * 差异明细导出Excel
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @param billId
     * @param request
     * @param response
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/diff/exp/excel")
    @ResponseBody
    public ResultEntity diffExpExcel(@RequestParam(name = "uid", defaultValue = "") String uid,
                                     @RequestParam(name = "token", defaultValue = "") String token,
                                     @RequestParam(name = "start", defaultValue = "") String start,
                                     @RequestParam(name = "limit", defaultValue = "") String limit,
                                     @RequestParam(name = "billId", defaultValue = "") String billId,
                                     HttpServletRequest request, HttpServletResponse response,
                                     @RequestBody Map data) throws Exception {
        XSSFWorkbook wb = this.billService.diffExpExcel(uid,billId,data);
        String path = request.getSession().getServletContext().getRealPath(".");
        String fileId = StringUtil.UUID();
        path =   path + File.separator + "upload";

        File filepack = new File(path);
        if(!filepack.exists()){
            filepack.mkdir();
        }
        String filePath =   path + File.separator + fileId + ".xlsx";
        File file = new File(filePath);
        file.createNewFile();
        FileOutputStream fileOut = new FileOutputStream(file);
        wb.write(fileOut);
        fileOut.close();
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setData(fileId);
        res.setMsg("文件生成成功！");
        return res;
    }





    /**
     * 移除保单
     * @param uid
     * @param token
     * @param data
     * @return
     */
    @RequestMapping("/remove")
    @ResponseBody
    public ResultEntity removeDetail(@RequestParam(name = "uid", defaultValue = "") String uid,
                                     @RequestParam(name = "token", defaultValue = "") String token,
                                     @RequestBody List<Map> data) throws Exception {
        ResultEntity res=new ResultEntity();
        billService.removeDetailById(uid,data);
        res.setSuccess(true);
        res.setMsg("移除保单成功!!");
        return res;
    }
}
